package com.sun.jdk.date.jdk8after;

import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;


public class Practise {

    /**
     * 练习：使用三种方式计算2019年7月19日14时38分34秒后的3年7个月18天后是什么时候
     */
    private static void test1() {
        // 封装时间
        LocalDateTime localDateTime = LocalDateTime.of(2019, 7, 19, 14, 38, 34);
        System.out.println("localDateTime = " + localDateTime);

        // 计算3年7个月18天后是什么时候
        LocalDateTime resultTime1 = localDateTime.plusYears(3).plusMonths(7).plusDays(18);
        System.out.println("resultTime1 = " + resultTime1);

        Period period = Period.of(3, 7, 18);
        LocalDateTime resultTime2 = localDateTime.plus(period);
        System.out.println("resultTime2 = " + resultTime2);

        LocalDateTime resultTime3 = localDateTime.plus(3, ChronoUnit.YEARS).plus(7, ChronoUnit.MONTHS).plus(18, ChronoUnit.DAYS);
        System.out.println("resultTime3 = " + resultTime3);
    }

    /**
     * 将1998年3月18日17时19分28秒转换成以下格式：1998-03月-18---17:19分28秒
     */
    private static void test2() {
        // 从别处获得一个字符串类型的日期,其格式时xxxx年xx月xx日xx时xx分xx秒(如1998年3月18日17时19分28秒，1998年11月18日17时9分28秒)[其中年份必须是全写，不能简写，即1998年不能写成98年]
        String strDate = getStrDate();

        String Year = getElementOfDate(strDate, ElementOfDate.YEAR);
        String Month = getElementOfDate(strDate, ElementOfDate.MONTH);
        String Day = getElementOfDate(strDate, ElementOfDate.DAY);
        String Hour = getElementOfDate(strDate, ElementOfDate.HOUR);
        String Minute = getElementOfDate(strDate, ElementOfDate.MINUTE);
        String Second = getElementOfDate(strDate, ElementOfDate.SECOND);

        String pattern = getPattern(Year, Month, Day, Hour, Minute, Second);
        System.out.println("pattern = " + pattern);

        LocalDateTime localDateTime = LocalDateTime.parse(strDate, DateTimeFormatter.ofPattern(pattern));
        String formatLocalDateTime = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM月-dd---HH:mm分ss秒"));
        System.out.println("formatLocalDateTime = " + formatLocalDateTime);
    }

    private static String getPattern(String patternYear, String patternMonth, String patternDay, String patternHour, String patternMinute, String patternSecond) {
        StringBuilder pattern = new StringBuilder();

        // int yearLength = patternYear.length();
        // StringBuilder patternOfYear = new StringBuilder();
        // for (int i = 0; i < yearLength; i++) {
        //     patternOfYear.append("y");
        // }
        // pattern.append("年");

        if(patternYear.length() == 4){
            pattern.append("yyyy年");
        }else {
            pattern.append("yy年");
        }

        if (patternMonth.length() == 2) {
            pattern.append("MM月");
        } else {
            pattern.append("M月");
        }

        if (patternDay.length() == 2) {
            pattern.append("dd日");
        } else {
            pattern.append("d日");
        }

        if (patternHour.length() == 2) {
            pattern.append("HH时");
        } else {
            pattern.append("H时");
        }

        if (patternMinute.length() == 2) {
            pattern.append("mm分");
        } else {
            pattern.append("m分");
        }

        if (patternSecond.length() == 2) {
            pattern.append("ss秒");
        } else {
            pattern.append("s秒");
        }
        return pattern.toString();
    }

    private static String getStrDate() {
        return "1998年03月18日17时19分28秒";
        // return "1998年3月18日17时19分28秒";
        // return "1998年3月1日7时9分8秒";
    }

    private static String getElementOfDate(String strDate, ElementOfDate elementOfDate) {
        String element = "";
        switch (elementOfDate) {
            case YEAR:
                element = strDate.substring(0, strDate.indexOf("年"));
                break;
            case MONTH:
                element = strDate.substring(strDate.indexOf("年") + 1, strDate.indexOf("月"));
                break;
            case DAY:
                element = strDate.substring(strDate.indexOf("月") + 1, strDate.indexOf("日"));
                break;
            case HOUR:
                element = strDate.substring(strDate.indexOf("日") + 1, strDate.indexOf("时"));
                break;
            case MINUTE:
                element = strDate.substring(strDate.indexOf("时") + 1, strDate.indexOf("分"));
                break;
            default:
                element = strDate.substring(strDate.indexOf("分") + 1, strDate.indexOf("秒"));
        }
        return element;
    }

    public static void main(String[] args) {
        // test1();
        // System.out.println("----");
        test2();

    }
}

enum ElementOfDate {
    YEAR,
    MONTH,
    DAY,
    HOUR,
    MINUTE,
    SECOND
}